package team.seekers.table.utils;

import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Component;
import team.seekers.table.entity.vo.Classtable;


import java.util.Arrays;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Godc
 * @description
 */
@Component
public class ParseCourse {
//    《毛泽东思想和中国特色社会主义理论体系概论》下(391023.I4) (朱意) (9,北2_102)
    public static Classtable parseCourse(String text,String schoolYear, String term, int semesterId){
        System.out.println(text);
        if(text.equals("") ||text==null){
            return null;
        }

        String name = null;
        String teacher = null;
        String time;
        String place = null;
        int[] week = new int[20];
        // 拿到课程名称
        String[] s = text.split(" ");
        name = s[0];
        // 拆分课程的正则
        String regex = "([《》\\u4e00-\\u9fa5A-Za-z\\(\\)I]){1,50}\\([0-9.a-zA-Z]{1,30}\\)( \\(([\\u4e00-\\u9fa5]){2,4}\\))? \\([\\u4e00-\\u9fa50-9- ,a-zA-Z_]{1,20}\\)";
        // 拿到老师
        Pattern compile = Pattern.compile("\\(([\\u4e00-\\u9fa5]){2,4}\\)");
        Matcher matcher = compile.matcher(text);
        if(matcher.find()){
            teacher = matcher.group();
        }
        // 拿位置
        int i = text.lastIndexOf("(");
        int j = text.lastIndexOf(",");
        if(j<0){
            j = text.length()-1;
        }else{
            place = text.substring(j+1,text.length()-1);
        }
        // 拿时间
        time = text.substring(i+1, j);
        String[] s1 = time.split(" ");
        for (String s2 : s1) {
            if (s2.indexOf("-")==-1) {
                Integer num = Integer.valueOf(s2);
                week[num] = 1;
            }else{
                int start;
                int x = s2.indexOf("-");
                Integer y = Integer.valueOf(s2.substring(x + 1, s2.length()));
                int step = 1;
                if(s2.charAt(0)=='单'||s2.charAt(0)=='双'){
                    start = Integer.valueOf(s2.substring(1,x));
                    step = 2;
                }else{
                    start = Integer.valueOf(s2.substring(0,x));
                }
                for(i = start;i<=y;i+=step){
                    week[i] = 1;
                }
            }

        }
        UUID uuid = UUID.randomUUID();
        String id = uuid.toString();
        Classtable classtable = new Classtable();
        classtable.setId(id);
        classtable.setSchoolYear(schoolYear);
        classtable.setTerm(term);
        classtable.setName(name);
        classtable.setTeacher(teacher);
        classtable.setPlace(place);
        classtable.setWeek(week);
        classtable.setSemesterId(semesterId);
        return classtable;
    }

}
